<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;


class SystemStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
            'shopowner' => 'required',
            'introduction' => 'required',
            'phone' => 'required',
            'address' => 'required',
            'detailed_address' => 'required',
            'image' => 'required',
            'valid_time' => 'required',
            'day_time' => 'required',
            'day_time2' => 'required',
            'is_business' => 'required',
        ];
    }

    public function messages()
    {
        return [
            'name.required' => "门店名称不能为空",
            'shopowner.required' => '门店店长不能为空',
            'introduction.required' => "简介不能为空",
            'phone.required' => "手机号码不能为空",
            'address.required' => "省市区不能为空",
            'detailed_address.required' => "详细地址不能为空",
            'image.required' => "门店logo不能为空",
            'valid_time.required' => "核销有效日期不能为空",
            'day_time.required' => "每日营业开始时间不能为空",
            'day_time2.required' => "每日营业结束时间不能为空",
            'is_business.required' => "是否营业不能为空",
        ];
    }

    /**
     * 兼容 form 表单请求与 ajax 请求或者 json api 请求
     * 验证失败，返回错误信息
     *
     * @param Validator $validator
     * @throws
     */
    protected function failedValidation(Validator $validator)
    {

        throw (new HttpResponseException(response()->json([
            'code' => 5000,
            'msg' => $validator->errors()->first(),
            'data' => '',
        ])));
    }
}
